
<h1><span class="yiyi-st" id="yiyi-12">numpy.testing.assert_array_almost_equal</span></h1>
        <blockquote>
        <p>原文：<a href="https://docs.scipy.org/doc/numpy/reference/generated/numpy.testing.assert_array_almost_equal.html">https://docs.scipy.org/doc/numpy/reference/generated/numpy.testing.assert_array_almost_equal.html</a></p>
        <p>译者：<a href="https://github.com/wizardforcel">飞龙</a> <a href="http://usyiyi.cn/">UsyiyiCN</a></p>
        <p>校对：（虚位以待）</p>
        </blockquote>
    
<dl class="function">
<dt id="numpy.testing.assert_array_almost_equal"><span class="yiyi-st" id="yiyi-13"> <code class="descclassname">numpy.testing.</code><code class="descname">assert_array_almost_equal</code><span class="sig-paren">(</span><em>x</em>, <em>y</em>, <em>decimal=6</em>, <em>err_msg=&apos;&apos;</em>, <em>verbose=True</em><span class="sig-paren">)</span><a class="reference external" href="http://github.com/numpy/numpy/blob/v1.11.3/numpy/testing/utils.py#L815-L918"><span class="viewcode-link">[source]</span></a></span></dt>
<dd><p><span class="yiyi-st" id="yiyi-14">如果两个对象不等于所需精度，则引发AssertionError。</span></p>
<div class="admonition note">
<p class="first admonition-title"><span class="yiyi-st" id="yiyi-15">注意</span></p>
<p class="last"><span class="yiyi-st" id="yiyi-16">建议使用<a class="reference internal" href="numpy.testing.assert_allclose.html#numpy.testing.assert_allclose" title="numpy.testing.assert_allclose"><code class="xref py py-obj docutils literal"><span class="pre">assert_allclose</span></code></a>，<a class="reference internal" href="numpy.testing.assert_array_almost_equal_nulp.html#numpy.testing.assert_array_almost_equal_nulp" title="numpy.testing.assert_array_almost_equal_nulp"><code class="xref py py-obj docutils literal"><span class="pre">assert_array_almost_equal_nulp</span></code></a>或<a class="reference internal" href="numpy.testing.assert_array_max_ulp.html#numpy.testing.assert_array_max_ulp" title="numpy.testing.assert_array_max_ulp"><code class="xref py py-obj docutils literal"><span class="pre">assert_array_max_ulp</span></code></a>之一来代替此函数，以获得更一致的浮点比较。</span></p>
</div>
<p><span class="yiyi-st" id="yiyi-17">测试验证相同的形状并且用<code class="docutils literal"><span class="pre">abs（期望 - 实际）</span> <span class="pre"><span class="pre">0.5</span> <span class="pre">*</span> <span class="pre">10 **（ - 十进制）</span></span></code>。</span></p>
<p><span class="yiyi-st" id="yiyi-18">给定两个array_like对象，检查形状是否相等，并且这些对象的所有元素几乎相等。</span><span class="yiyi-st" id="yiyi-19">在形状不匹配或冲突值时引发异常。</span><span class="yiyi-st" id="yiyi-20">与numpy中的标准用法相反，NaNs是以数字进行比较的，如果两个对象在相同位置具有NaN，则不会产生断言。</span></p>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name">
<col class="field-body">
<tbody valign="top">
<tr class="field-odd field"><th class="field-name"><span class="yiyi-st" id="yiyi-21">参数：</span></th><td class="field-body"><p class="first"><span class="yiyi-st" id="yiyi-22"><strong>x</strong>：array_like</span></p>
<blockquote>
<div><p><span class="yiyi-st" id="yiyi-23">要检查的实际对象。</span></p>
</div></blockquote>
<p><span class="yiyi-st" id="yiyi-24"><strong>y</strong>：array_like</span></p>
<blockquote>
<div><p><span class="yiyi-st" id="yiyi-25">期望的，期望的对象。</span></p>
</div></blockquote>
<p><span class="yiyi-st" id="yiyi-26"><strong>十进制</strong>：int，可选</span></p>
<blockquote>
<div><p><span class="yiyi-st" id="yiyi-27">所需的精度，默认为6。</span></p>
</div></blockquote>
<p><span class="yiyi-st" id="yiyi-28"><strong>err_msg</strong>：str，可选</span></p>
<blockquote>
<div><p><span class="yiyi-st" id="yiyi-29">出现故障时打印的错误消息。</span></p>
</div></blockquote>
<p><span class="yiyi-st" id="yiyi-30"><strong>verbose</strong>：bool，可选</span></p>
<blockquote>
<div><p><span class="yiyi-st" id="yiyi-31">如果为True，则冲突的值将附加到错误消息。</span></p>
</div></blockquote>
</td>
</tr>
<tr class="field-even field"><th class="field-name"><span class="yiyi-st" id="yiyi-32">上升：</span></th><td class="field-body"><p class="first"><span class="yiyi-st" id="yiyi-33"><strong>AssertionError</strong></span></p>
<blockquote class="last">
<div><p><span class="yiyi-st" id="yiyi-34">如果实际和期望的不等于指定的精度。</span></p>
</div></blockquote>
</td>
</tr>
</tbody>
</table>
<div class="admonition seealso">
<p class="first admonition-title"><span class="yiyi-st" id="yiyi-35">也可以看看</span></p>
<dl class="docutils">
<dt><span class="yiyi-st" id="yiyi-36"><a class="reference internal" href="numpy.testing.assert_allclose.html#numpy.testing.assert_allclose" title="numpy.testing.assert_allclose"><code class="xref py py-obj docutils literal"><span class="pre">assert_allclose</span></code></a></span></dt>
<dd><span class="yiyi-st" id="yiyi-37">将两个array_like对象与所需的相对和/或绝对精度进行比较。</span></dd>
</dl>
<p class="last"><span class="yiyi-st" id="yiyi-38"><a class="reference internal" href="numpy.testing.assert_array_almost_equal_nulp.html#numpy.testing.assert_array_almost_equal_nulp" title="numpy.testing.assert_array_almost_equal_nulp"><code class="xref py py-obj docutils literal"><span class="pre">assert_array_almost_equal_nulp</span></code></a>，<a class="reference internal" href="numpy.testing.assert_array_max_ulp.html#numpy.testing.assert_array_max_ulp" title="numpy.testing.assert_array_max_ulp"><code class="xref py py-obj docutils literal"><span class="pre">assert_array_max_ulp</span></code></a>，<a class="reference internal" href="numpy.testing.assert_equal.html#numpy.testing.assert_equal" title="numpy.testing.assert_equal"><code class="xref py py-obj docutils literal"><span class="pre">assert_equal</span></code></a></span></p>
</div>
<p class="rubric"><span class="yiyi-st" id="yiyi-39">例子</span></p>
<p><span class="yiyi-st" id="yiyi-40">第一个断言不会引发异常</span></p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">np</span><span class="o">.</span><span class="n">testing</span><span class="o">.</span><span class="n">assert_array_almost_equal</span><span class="p">([</span><span class="mf">1.0</span><span class="p">,</span><span class="mf">2.333</span><span class="p">,</span><span class="n">np</span><span class="o">.</span><span class="n">nan</span><span class="p">],</span>
<span class="go">                                         [1.0,2.333,np.nan])</span>
</pre></div>
</div>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">np</span><span class="o">.</span><span class="n">testing</span><span class="o">.</span><span class="n">assert_array_almost_equal</span><span class="p">([</span><span class="mf">1.0</span><span class="p">,</span><span class="mf">2.33333</span><span class="p">,</span><span class="n">np</span><span class="o">.</span><span class="n">nan</span><span class="p">],</span>
<span class="gp">... </span>                                     <span class="p">[</span><span class="mf">1.0</span><span class="p">,</span><span class="mf">2.33339</span><span class="p">,</span><span class="n">np</span><span class="o">.</span><span class="n">nan</span><span class="p">],</span> <span class="n">decimal</span><span class="o">=</span><span class="mi">5</span><span class="p">)</span>
<span class="gp">...</span>
<span class="go">&lt;type &apos;exceptions.AssertionError&apos;&gt;:</span>
<span class="go">AssertionError:</span>
<span class="go">Arrays are not almost equal</span>

<span class="go">(mismatch 50.0%)</span>
<span class="go"> x: array([ 1.     ,  2.33333,      NaN])</span>
<span class="go"> y: array([ 1.     ,  2.33339,      NaN])</span>
</pre></div>
</div>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">np</span><span class="o">.</span><span class="n">testing</span><span class="o">.</span><span class="n">assert_array_almost_equal</span><span class="p">([</span><span class="mf">1.0</span><span class="p">,</span><span class="mf">2.33333</span><span class="p">,</span><span class="n">np</span><span class="o">.</span><span class="n">nan</span><span class="p">],</span>
<span class="gp">... </span>                                     <span class="p">[</span><span class="mf">1.0</span><span class="p">,</span><span class="mf">2.33333</span><span class="p">,</span> <span class="mi">5</span><span class="p">],</span> <span class="n">decimal</span><span class="o">=</span><span class="mi">5</span><span class="p">)</span>
<span class="go">&lt;type &apos;exceptions.ValueError&apos;&gt;:</span>
<span class="go">ValueError:</span>
<span class="go">Arrays are not almost equal</span>
<span class="go"> x: array([ 1.     ,  2.33333,      NaN])</span>
<span class="go"> y: array([ 1.     ,  2.33333,  5.     ])</span>
</pre></div>
</div>
</dd></dl>
